package org.liuhy.line
{
	import flash.display.DisplayObject;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import mx.core.UITextField;
	
	import org.liuhy.node.FlowNode;
	import org.liuhy.utils.LineUtils;
	
	public class CLine extends UIComponent
	{
		private var _lineList:ArrayCollection = new ArrayCollection();
		private var _text:TextInput = new TextInput();
		private var _lineColor:uint					 = 0x000000;
		private var _lineNodeBorderColor:uint		 = 0x000000;
		private var _lineNodeBackgroundColor:uint	 = 0xFFFFFF;
		private var _type:String = "STRAIGHT_LINE";
		public var _selected:Boolean = false;
//		public function getCLinePosStrs():String{
//			var _str:String = "";
//			var _line:AbstractLine = this.getFromLine();
//			while(null!=_line){
//				if(null!=_line.nextLine){
//					_str = _str + (this.x + _line.x1) +  "," + (this.y + _line.y1) + ";" ;
//				}else{
//					_str = _str + (this.x + _line.x1) +  "," + (this.y + _line.y1) + ";" ;
//					_str = _str + (this.x + _line.x2) +  "," + (this.y + _line.y2) + ";" ;
//				}
//				_line=_line.nextLine;
//			}
//			return _str;			 
//		}
		public function CLine()
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
			this.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				event.stopPropagation();
			});

			this.addEventListener(MouseEvent.MOUSE_UP,stopDragging);
			this.addEventListener(KeyboardEvent.KEY_DOWN,onDelete);
			this.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE,onFocusChange);
			this.doubleClickEnabled = true;
			this.addEventListener(MouseEvent.DOUBLE_CLICK,onCLindeDoubleClick);
			this._text.addEventListener(MouseEvent.MOUSE_DOWN,function(event:MouseEvent):void{
				event.stopPropagation();
			});
			this._text.addEventListener(MouseEvent.MOUSE_MOVE,function(event:MouseEvent):void{
				event.stopPropagation();
			});
			this._text.addEventListener(MouseEvent.CLICK,onTextClick);
			this._text.addEventListener(MouseEvent.DOUBLE_CLICK,onTextDoubleClick);
			this._text.addEventListener(TextEvent.TEXT_INPUT,onTextInput);
			this._text.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE,onTextFocusChange);
			this._text.height = 22;
			this.addChild(this._text);
		}

		public function onTextFocusChange(event:FocusEvent):void{
			var _txt:TextInput = event.currentTarget as TextInput;
			this.adjustTextPosition(_txt);
			if(""==_txt.text) _txt.visible = false;
		}
		public function onTextInput(event:TextEvent):void{
			this.adjustTextPosition(event.target as TextInput);
		}
		public function onTextClick(event:MouseEvent):void{
			event.stopPropagation();				
		}
		public function onTextDoubleClick(event:MouseEvent):void{
			event.stopPropagation();
			this._text.stage.focus = this._text;	
			this.setChildIndex(this._text,this.numChildren-1);		
		}
		public function onCLindeDoubleClick(event:MouseEvent):void{
			this._text.visible = true;
			var _point:Point = this.getLabelPos();
			if(this._text.text.length<5){
				this._text.width = 80;
			}else{
				this._text.width  = this._text.text.length*14+10;
			}
			this._text.x = _point.x - this._text.width/2;
			this._text.y = _point.y - this._text.height/2;
			this._text.setStyle("textAlign","center");
			this._text.setStyle("borderStyle","solid");			
			this._text.setStyle("borderThickness","0.2");
			this.setChildIndex(this._text,this.numChildren-1);	
		}
		public function adjustTextPosition(p_text:TextInput):void{
			var _point:Point = this.getLabelPos();
			if(p_text.text.length<5){
				p_text.width = 80;
			}else{
				p_text.width  = p_text.text.length*14+10;
			}
			p_text.x = _point.x - p_text.width/2;
			p_text.y = _point.y - p_text.height/2;
			p_text.setStyle("textAlign","center");
			p_text.setStyle("borderStyle","solid");
			p_text.setStyle("borderThickness","0.2");			
		}
		public function adjustTitle(event:MouseEvent):void{
			var _point:Point = this.getLabelPos();
			if(this._text.text.length<5){
				this._text.width = 80;
			}else{
				this._text.width  = this._text.text.length*14;
			}
			this._text.x = _point.x - this._text.width/2;
			this._text.y = _point.y - this._text.height/2;
			this._text.setStyle("textAlign","center");
			this._text.setStyle("borderStyle","solid");
			this._text.setStyle("borderThickness","0.2");
			if(""==this._text.text) this._text.visible=false;
			this.setChildIndex(this._text,this.numChildren-1);						
		}
		public function draw():void{
			for(var i:int=0;i<_lineList.length;i++){
				var _line:AbstractLine = _lineList.getItemAt(i) as AbstractLine;
				_line.lineColor = this._lineColor;
				_line.lineNodeBorderColor = this._lineNodeBorderColor;
				_line.lineNodeBackgroundColor = this._lineNodeBackgroundColor;
				_line.draw();
			}
		}	
		public function getCLineLen():int{
			var _len:int = 0;
			for(var i:int=0;i<_lineList.length;i++){
				var _line:AbstractLine = _lineList.getItemAt(i) as AbstractLine;
				_len = _len + _line.getLineLen();				
			}
			return _len;
		}
		//获取文本标签所在位置;
		public function getLabelPos():Point{
			//放在线段长度中间位置;
			//计算每根线段长度；
			var _len:int = this.getCLineLen();
			//获取from线段;
			var _uLine:AbstractLine = this.getFromLine();
			var _c_len:int = 0 ;
			while(null!=_uLine){
				if(_c_len+_uLine.getLineLen()>=_len/2){
					var _x_:int = 0;
					var _y_:int = 0;
					if(_uLine.x1==_uLine.x2){
						_x_ = _uLine.x1;
						if(_uLine.y1<_uLine.y2){
							//y1在上;
							_y_=_uLine.y1 + (_len/2-_c_len);
						}else{
							//y1在下;
							_y_=_uLine.y1 -(_len/2-_c_len);
						}
					}
					if(_uLine.y1==_uLine.y2){
						_y_ = _uLine.y1;
						if(_uLine.x1<_uLine.x2){
							//x1在上;
							_x_=_uLine.x1 + (_len/2-_c_len);
						}else{
							//x1在下;
							_x_=_uLine.x1 -(_len/2-_c_len);
						}
					}
					return new Point(_x_,_y_);
				}
				_c_len = _c_len	+	_uLine.getLineLen();
				_uLine = _uLine.nextLine;
			}
			return null;
		}
		public function initDraw():void{
			for(var i:int=0;i<_lineList.length;i++){
				var _line:AbstractLine = _lineList.getItemAt(i) as AbstractLine;
				if(i>0){					
					_line.preLine  = _lineList.getItemAt(i-1) as AbstractLine;
				}
				if(i+1<_lineList.length){
					_line.nextLine = _lineList.getItemAt(i+1) as AbstractLine;
				}
				_line.draw();
			}
		}
		
		private function onClick(event:MouseEvent):void{
//			this.setChildIndex(this._text,0);
//			this.stage.focus = this;
		}
		public function setLineFocusState(p_bool:Boolean):void{
			for(var i:int=0;i<_lineList.length;i++){
				var _line:AbstractLine 		  = _lineList.getItemAt(i) as AbstractLine;
				_line.lineColor				  = this._lineColor;
				_line.lineNodeBorderColor 	  = this._lineNodeBorderColor;
				_line.lineNodeBackgroundColor = this._lineNodeBackgroundColor;
				_line.draw();
			}
		}
		public function onLineFocus(event:MouseEvent):void{	
				_selected = !_selected;	
				setFocusVisible(_selected);
				this.lineColor 					= 0x000000;
				this.lineNodeBorderColor		= 0x00FF00;
				this.lineNodeBackgroundColor 	= 0x00FF00;
				//调整线段选中颜色;
				setLineFocusState(_selected);			
				var _canvas:Canvas = this.parent as Canvas;
				//设置当前选中线在面板最前面;			
				_canvas.setChildIndex(this,_canvas.numChildren-1);
				this.setChildIndex(this._text,0);
				this.stage.focus = this;
				event.stopPropagation();
		}
		public function onFocusChange(event:FocusEvent):void{
//			this.lineColor 				= 0x000000;
//			this.lineNodeBorderColor	= 0x000000;
//			this.lineNodeBackgroundColor= 0xFFFFFF;
//			this.setLineFocusState(false);
//			this.setChildIndex(this._text,this.numChildren-1);
		}
		public function setFocusVisible(p_bln:Boolean):void{			
			for(var j:int=0;j<_lineList.length;j++){
				if(_lineList.getItemAt(j) is AbstractLine){
					var _line:AbstractLine = _lineList.getItemAt(j) as AbstractLine;
					_line.setFocusVisible(p_bln);
				}
			}			
		}
		private function onDelete(event:KeyboardEvent):void{
			if(event.target is UITextField) return;
			if(127==event.charCode){
				var _c:Canvas = this.parent as Canvas;
				if(null!=this.getArrowFlowNode()){
					this.getArrowFlowNode().removeInLine(this.getArrowLine());
				}
				if(null!=this.getFromFlowNode()){
					this.getFromFlowNode().removeOutLine(this.getFromLine());
				}
				_c.removeChild(this);
			}
		}
		public function getArrowLine():AbstractLine{
			if(_lineList.length<1) return null;
			return LineUtils.getArrowLine(_lineList.getItemAt(0) as AbstractLine);
		}
		public function getFromLine():AbstractLine{
			if(_lineList.length<1) return null;
			return LineUtils.getFromLine(_lineList.getItemAt(0) as AbstractLine);
		}
		//获取来源节点;
		public function getFromFlowNode():FlowNode{
			var _line:AbstractLine = getFromLine();
			if(null!=_line){
				if(null!=_line.out_flowFocusNode){
					return _line.out_flowFocusNode.parent as FlowNode;
				}
			}
			return null;
		}
		//获取箭头指向节点;
		public function getArrowFlowNode():FlowNode{
			var _line:AbstractLine = getArrowLine();
			if(null!=_line){
				if(null!=_line.in_flowFocusNode){
					return _line.in_flowFocusNode.parent as FlowNode;
				}
			}
			return null;
		}
		override public function addChild(p_child:DisplayObject):DisplayObject{
			if(p_child is AbstractLine){
				_lineList.addItem(p_child);
			}
			return super.addChild(p_child);
		}
		override public function removeChild(p_child:DisplayObject):DisplayObject{
			for(var i:int=0;i<_lineList.length;i++){
				if(p_child==_lineList.getItemAt(i)){
					_lineList.removeItemAt(i);
					break;
				}
			}
			if(super.contains(p_child)){
				return super.removeChild(p_child);
			}else{
				return null;
			}
		}
		public function get lineList():ArrayCollection{
			return this._lineList;
		}
		public function startDragging(event:MouseEvent):void{	
			onLineFocus(event);		
			event.currentTarget.startDrag();
		}
		public function stopDragging(event:MouseEvent):void{
			event.currentTarget.stopDrag();
		}
		public function get title():String{
			return this._text.text;
		}
		public function set title(p_title:String):void{
			this._text.text = p_title;
		}
		public function set lineColor(p_color:uint):void{
			this._lineColor = p_color;
		}
		public function get lineColor():uint{
			return this._lineColor;
		}
		public function set lineNodeBorderColor(p_color:uint):void{
			this._lineNodeBorderColor = p_color;
		}
		public function get lineNodeBorderColor():uint{
			return this._lineNodeBorderColor;
		}
		public function set lineNodeBackgroundColor(p_color:uint):void{
			this._lineNodeBackgroundColor = p_color;
		}
		public function get lineNodeBackgroundColor():uint{
			return this._lineNodeBackgroundColor;
		}
	}
}